package yb.ecp.fast.user.service.VO;

import org.apache.commons.collections.CollectionUtils;
import yb.ecp.fast.user.dao.entity.AuthDO;
import yb.ecp.fast.user.dao.entity.MenuDO;
import yb.ecp.fast.user.dao.entity.TmpAuthDO;
import yb.ecp.fast.user.dao.entity.TmpMenuDO;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName AuthorizedQueryParamCache
 * @Description 权限查询时，菜单，模板，权限等表的全量数据，主要作为参数使用
 * @author wzy
 * @date 2020-08-27 11:36
 */
public class AuthorizedQueryParamCache {

    List<MenuDO> allMenues;
    List<TmpMenuDO> allTmpMenues;
    List<TmpAuthDO> allTmpAuths;
    List<AuthDO> menuAuths;

    //按照parentId分组的菜单数据
    Map<Long, List<MenuDO>> parentGroupMenuesMap;
    //按照site分组的菜单数据
    Map<Integer, List<MenuDO>> siteGroupMenuesMap ;
    //按照tempI分组的TmpMenu数据
    Map<String, List<TmpMenuDO>> tmpIdGroupTmpMenuesMap;
    //按照tmpId分组TmpAuth数据
    Map<String,List<TmpAuthDO>> tmpIdGroupTmpAuth;
    //按照菜单id分组的菜单权限
    Map<String, List<AuthDO>> menuidGroupMenuAuths;

    public AuthorizedQueryParamCache(){}

    public AuthorizedQueryParamCache(List<MenuDO> allMenues,
            List<TmpMenuDO> allTmpMenues,
            List<TmpAuthDO> allTmpAuths,
            List<AuthDO> menuAuths){
        this.allMenues = allMenues;
        this.allTmpMenues = allTmpMenues;
        this.allTmpAuths = allTmpAuths;
        this.menuAuths = menuAuths;

        this.parentGroupMenuesMap = groupMunesByParentIdSorted(allMenues);
        this.siteGroupMenuesMap = groupMunesBySiteSorted(allMenues);
        this.tmpIdGroupTmpMenuesMap = groupTmpMenuByTempId(allTmpMenues);
        this.tmpIdGroupTmpAuth =groupTmpAuthByTmpId(allTmpAuths);
        this.menuidGroupMenuAuths = groupMenuAuthsByMenuId(menuAuths);
    }



    //按照tmpId分组TmpAuth数据
    private Map<String,List<TmpAuthDO>> groupTmpAuthByTmpId(List<TmpAuthDO> allTmpAuth){
        if(CollectionUtils.isEmpty(allTmpAuth)){
            return Collections.EMPTY_MAP;
        }
        Map<String, List<TmpAuthDO>> allTmpAuthTmpIdGroup = allTmpAuth.stream().collect(Collectors.groupingBy(TmpAuthDO::getTmpId));
        return allTmpAuthTmpIdGroup;
    }

    //按照menuId分组菜单权限
    private Map<String,List<AuthDO>> groupMenuAuthsByMenuId(List<AuthDO> auths){
        if(CollectionUtils.isEmpty(auths)){
            return Collections.EMPTY_MAP;
        }
        //按照menuId分组
        Map<String, List<AuthDO>> allAuthorMenuIdGroup = auths.stream().collect(Collectors.groupingBy(AuthDO::getMenuId));
        return allAuthorMenuIdGroup;
    }

    //按照parentId分组menues，并按照seq排序，排序为ORDER BY seq
    private Map<Long,List<MenuDO>> groupMunesByParentIdSorted(List<MenuDO> allMenues){
        if(CollectionUtils.isEmpty(allMenues)){
            return Collections.EMPTY_MAP;
        }
        //按照parentId分组
        Map<Long, List<MenuDO>> allMenuesParentGroup = allMenues.stream().collect(Collectors.groupingBy(MenuDO::getParentId));
        //TODO 排序，按照seq排序
        return allMenuesParentGroup;
    }

    //按照site分组，并排序，排序为ORDER BY parent_id,seq
    private Map<Integer,List<MenuDO>> groupMunesBySiteSorted(List<MenuDO> allMenues){
        if(CollectionUtils.isEmpty(allMenues)){
            return Collections.EMPTY_MAP;
        }
        //按照parentId分组
        Map<Integer, List<MenuDO>> allMenuesSiteGroup = allMenues.stream().collect(Collectors.groupingBy(MenuDO::getSite));
        //TODO 排序，ORDER BY parent_id,seq
        return allMenuesSiteGroup;
    }


    //按照tmpId分组
    private Map<String, List<TmpMenuDO>> groupTmpMenuByTempId(List<TmpMenuDO> allTmpMenues){
        if(CollectionUtils.isEmpty(allTmpMenues)){
            return Collections.EMPTY_MAP;
        }
        Map<String, List<TmpMenuDO>> tmpMenuesGroup = allTmpMenues.stream().collect(Collectors.groupingBy(TmpMenuDO::getTmpId));
        return tmpMenuesGroup;
    }

    public Map<Long, List<MenuDO>> getParentGroupMenuesMap() {
        return parentGroupMenuesMap;
    }

    public void setParentGroupMenuesMap(Map<Long, List<MenuDO>> parentGroupMenuesMap) {
        this.parentGroupMenuesMap = parentGroupMenuesMap;
    }

    public Map<Integer, List<MenuDO>> getSiteGroupMenuesMap() {
        return siteGroupMenuesMap;
    }

    public void setSiteGroupMenuesMap(Map<Integer, List<MenuDO>> siteGroupMenuesMap) {
        this.siteGroupMenuesMap = siteGroupMenuesMap;
    }

    public Map<String, List<TmpMenuDO>> getTmpIdGroupTmpMenuesMap() {
        return tmpIdGroupTmpMenuesMap;
    }

    public void setTmpIdGroupTmpMenuesMap(Map<String, List<TmpMenuDO>> tmpIdGroupTmpMenuesMap) {
        this.tmpIdGroupTmpMenuesMap = tmpIdGroupTmpMenuesMap;
    }

    public Map<String, List<TmpAuthDO>> getTmpIdGroupTmpAuth() {
        return tmpIdGroupTmpAuth;
    }

    public void setTmpIdGroupTmpAuth(Map<String, List<TmpAuthDO>> tmpIdGroupTmpAuth) {
        this.tmpIdGroupTmpAuth = tmpIdGroupTmpAuth;
    }

    public List<MenuDO> getAllMenues() {
        return allMenues;
    }

    public void setAllMenues(List<MenuDO> allMenues) {
        this.allMenues = allMenues;
    }

    public List<TmpMenuDO> getAllTmpMenues() {
        return allTmpMenues;
    }

    public void setAllTmpMenues(List<TmpMenuDO> allTmpMenues) {
        this.allTmpMenues = allTmpMenues;
    }

    public List<TmpAuthDO> getAllTmpAuths() {
        return allTmpAuths;
    }

    public void setAllTmpAuths(List<TmpAuthDO> allTmpAuths) {
        this.allTmpAuths = allTmpAuths;
    }

    public List<AuthDO> getMenuAuths() {
        return menuAuths;
    }

    public void setMenuAuths(List<AuthDO> menuAuths) {
        this.menuAuths = menuAuths;
    }

    public Map<String, List<AuthDO>> getMenuidGroupMenuAuths() {
        return menuidGroupMenuAuths;
    }

    public void setMenuidGroupMenuAuths(Map<String, List<AuthDO>> menuidGroupMenuAuths) {
        this.menuidGroupMenuAuths = menuidGroupMenuAuths;
    }
}
